from django.contrib.gis.db import models
from django.utils.translation import ugettext_lazy as _
from location import Location

class Organization(models.Model):
    __module__ = 'duetopia.register.models'
    name = models.CharField(max_length=256, verbose_name=_('name'))
    location = models.ForeignKey(Location, blank=True, null=True, verbose_name=_('location'))
    def __str__(self):
        return self.name
    class Admin:
        list_display = ['name']

    class Meta:
        verbose_name = _('organization')
        verbose_name_plural = _('organizations')


class Agent(models.Model):
    __module__ = 'duetopia.register.models'
    name = models.CharField(max_length=256, verbose_name=_('name'))
    email = models.EmailField(max_length=256, verbose_name=_('email'))
    organization = models.ForeignKey(Organization, blank=True, null=True, verbose_name=_('organization'))
    position = models.CharField(max_length=256, blank=True, null=True, verbose_name=_('position'))
    location = models.ForeignKey(Location, blank=True, null=True, verbose_name=_('location'))
    description = models.TextField(max_length=4096, blank=True, null=True, verbose_name=_('description'))
    class Admin:
        list_display = ('name','email')
        list_per_page = 15;
    class Meta:
        verbose_name = _('agent')
        verbose_name_plural = _('agents')
    def __str__(self):
        output = [u"%s <%s>" % (self.name, self.email,)]
        if self.position: output.append(u"%s"%(self.position))
        if self.organization: output.append(u"%s"%(self.organization.name))
        if self.location: output.append(u"%s"%(self.location))
        return u'\n'.join(output)

class Annotation(models.Model):
    __module__ = 'duetopia.register.models'
    note = models.CharField(max_length=16384, verbose_name=_('note'))
    def __str__(self):
        return unicode.encode(self.note, 'utf-8')

class Vocabulary(models.Model):
    """
    v = Vocabulary(name='Example terms',uri='http://example.com/ns')
    """
    __module__ = 'duetopia.register.models'
    name = models.CharField(max_length=256, verbose_name=_('name'))
    uri =  models.CharField(max_length=256, unique=True, verbose_name=_('URI'))
    class Admin:
        list_display = ('name','uri')

class Keyword(models.Model):
    """
    Keyword may have optional 'lang' and optional vocabulary, which
    should be a reference to a Vocabulary object or its ID.
    >>> k = Keyword(word='goose',lang='en')
    """
    __module__ = 'duetopia.register.models'
    word = models.CharField(max_length=256, verbose_name=_('word'))
    lang = models.CharField(max_length=256, blank=True, null=True, verbose_name=_('language'))
    vocabulary = models.ForeignKey(Vocabulary, blank=True, null=True)
    class Admin:
        list_display = ('word', 'lang')
    class Meta:
        verbose_name = _('keyword')
        verbose_name_plural = _('keywords')
        ordering = ('word',)
    def __str__(self):
            return unicode.encode(self.word, 'utf-8')

class License(models.Model):
    __module__ = 'duetopia.register.models'
    uri = models.CharField(max_length=512,null=True,verbose_name=_('uri'))
    description = models.TextField(max_length=4096, blank=True, null=True, verbose_name=_('description'))
    shortname = models.CharField(max_length=64,verbose_name=_('short name'))

    class Admin:
        list_display = ('shortname', 'description')
    class Meta:
        verbose_name = _('license')
        verbose_name_plural = _('licenses')
    def __str__(self):
        return self.shortname


